<?php
if (!(defined('IN_IA')))
{
    exit('Access Denied');
}
/**
 * User: 邓志锋 <280160522@qq.com> <http://www.diygw.com>
 * Date: 2018-07-28
 * Time: 下午 6:06
 */
class Order_Diygwcom extends Interceptor_Diygwcom
{
    public function index()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $title="我的订单";
        $extendValues = $this->getDashboardExtends($dashboardid);
        include $this->template();
    }

    public function confirm()
    {
        global $_GPC, $_W;
        $ids = $_GPC["ids"];
        $title="订单确认";
        $dashboardid = $_GPC["dashboardid"];
        $extendValues = $this->getDashboardExtends($dashboardid);
        include $this->template();
    }


    public function nav()
    {
        global $_GPC, $_W;
        $titles=["待付款","待发货","待收货","退款/售后"];
        $status=["1","2","3","10"];
        $rows=[];
        foreach ($titles as $index=>$title){
            $row['title']=$title;
            $row['status']=$status[$index];
            $rows[]=$row;
        }
        return $this->getPageMessage("加载数据成功",$rows,count($row));
    }

    public function pay()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $uid =$this->getUid();

        $cart['user_id']=$uid;
        $cart['dashboard_id']=$dashboardid;
        $ids=$_GPC["ids"];

        if (!empty($ids)) {
            if(is_array($ids)){
                $cart['id']=safe_gpc_array($ids);
            }else{
                $cart['id']=explode(",",$ids);
            }
        }
        $list = pdo_getall(getTableName("cart"),$cart);
        if(empty($list)){
           return $this->getErrorPage('购物车空空如也，不能发现支付');
        }
        $totalPrice=0;
        foreach ($list as $key=>$item){
            $totalPrice=$totalPrice+safe_gpc_int($item['link_price'])*safe_gpc_int($item['link_total']);
        }
        $__isxcx__ = $_GPC["__isxcx__"];
        $__isajax__ = $_GPC["__isajax__"];
        try{
            pdo()->begin();
            $order["user_id"]=$uid;
            $order["dashboard_id"]=$dashboardid;
            $order["order_id"]=DiygwcomUtil::create_guid();
            $order["pay_price"]=$totalPrice;
            $order["cart_price"]=$totalPrice;
            $order['create_time']=date("Y-m-d H:i:s", time());
            $order['update_time']=date("Y-m-d H:i:s", time());
            $order['pay_title']=$list[0]["link_title"];
            $order['client_remark']=safe_gpc_string($_GPC["remark"]);
            $order["cart_list"]=json_encode($list);

            $addressid=safe_gpc_string($_GPC["addressid"]);
            if(!empty($addressid)){
                $address= pdo_get(getTableName("address"),array("id"=>$addressid));
                $order['client_gender']=safe_gpc_string($address["gender"]);
                $order['client_name']=safe_gpc_string($address["name"]);
                $order['client_tel']=safe_gpc_string($address["tel"]);
                $order['client_address']=safe_gpc_string($address["address"]);
            }
            pdo_insert(getTableName("order"),$order);
            $orderid = pdo_insertid();
            $cartIds=[];
            foreach ($list as $key=>$item){
                $cartIds[]=$item['id'];
                $item["id"]=null;
                $item["order_id"]=$orderid;
                pdo_insert(getTableName("order_info"),$item);
            }
            pdo_delete(getTableName("cart"),array('id'=>$cartIds));

            pdo()->commit();
            if((!empty($__isajax__)&&$__isajax__=='true')||(!empty($__isxcx__)&&$__isxcx__=='1')){
                echo json_encode(["id"=>$order["order_Id"],"status"=>'success']);
            }else{
                header('location:' . DiygwcomUtil::to_mobile_url($this->createMobileUrl('pay_index', array('m'=>'diygwcom_app','id'=>$orderid,"dashboardid"=>$order["dashboard_id"]))));
            }
        }catch (Exception $e) {
            pdo()->rollback();
            var_dump($e);
            if((!empty($__isajax__)&&$__isajax__=='true')||(!empty($__isxcx__)&&$__isxcx__=='1')){
                $this->getErrorMessage('生成订单失败');
            }else{
                $this->getErrorPage('生成订单失败');
            }
            die;
        }
    }



    public function changeStatus()
    {
        global $_GPC, $_W;
        $id = safe_gpc_int($_GPC["id"]);
        $status = safe_gpc_int($_GPC["status"]);
        $order= pdo_get("order",array('id'=>$id));
        if($order['status']>=2){
            return $this->getErrorMessage("订单已支付成功，不能手动修改状态");
        }

        $result = pdo_update(getTableName("order"),array('status'=>$status),array('id'=>$id));
        if(false!==$result){
            return $this->getSuccessMessage("修改状态成功");
        }else{
            return $this->getSuccessMessage("修改状态失败");
        }
    }

    public function data()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $uid =$this->getUid();
        $status = safe_gpc_int($_GPC["status"]);

        $order['dashboard_id']=$dashboardid;
        $order['user_id']=$uid;
        $order['status']=$status;
        $list = pdo_getall(getTableName("order"),$order);
        return $this->getPageMessage("加载数据成功",$list,count($list));
    }

}